package com.example.hp.zyfypt330.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;


import com.example.hp.zyfypt330.R;
import com.example.hp.zyfypt330.adapter.MyAdapter;
import com.example.hp.zyfypt330.bean.ArticleBean;
import com.example.hp.zyfypt330.iface.ArticleListener;
import com.example.hp.zyfypt330.model.ArticleModel;

import java.util.List;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by Neusoft on 2017/4/9.
 */

public class Fragment1 extends Fragment {
    private TextView tvinfo;
    private View view = null;
    private Context context;

    //1copy by mainactivity
    private RecyclerView recyclerView;
    private LinearLayoutManager  layoutManager;//显示布局效果
    private MyAdapter myAdapter;
    private List<ArticleBean> list = null;
    private int page=1; // 代表页数，并初始化为1，代表第1页。
    private int lastVisibleItemPosition;//最后一条可见条目的位置
    private SharedPreferences sp;
    private String sessionID;

    private ArticleListener articleListener = new ArticleListener() {
        @Override
        public void onResponse(List<ArticleBean> beanlist) {
            if(page==1)
            {
                list=beanlist;
            }
            else {
                list.removeAll(beanlist);
                list.addAll(beanlist);
            }
            myAdapter.setList(list);//传给adapter
            myAdapter.notifyDataSetChanged();//通知更新
        }

        @Override
        public void onFail(String msg) {

            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        }
    };

    //2 copy by mainacivity
    private void initRecyclerView() {
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);//!
        //创建默认的线性LayoutManager
        layoutManager = new LinearLayoutManager(context);//!
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        recyclerView.setHasFixedSize(true);
        //创建并设置Adapter
        myAdapter = new MyAdapter(context);//!
        myAdapter.setList(list);
        recyclerView.setAdapter(myAdapter);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == list.size()) {
                    page += 1;
                    //再次实例化ArticleModel，调用方法获取网络数据，请求新一页数据
                    ArticleModel articleModel = new ArticleModel();
                    articleModel.getResultList("article",page,"k17vmm42ui9al6clh0lo3de2t7",articleListener);
                }
            }
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItemPosition=layoutManager.findLastVisibleItemPosition();//滚动结束后将赋值为可见条目中最后一条位置
            }
        });
    }




    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        context = getActivity();
        //动态加载Fragment1的布局文件
        view = inflater.inflate(R.layout.fragment1, container, false);
        //获取布局上的控件，注意通过view获取
        tvinfo = (TextView) view.findViewById(R.id.textView8);
        tvinfo.setText("文章");
        //注意Toast第一个参数上下文
        Toast.makeText(context, "加载Fragment文章", Toast.LENGTH_SHORT).show();
        sp=context.getSharedPreferences("login",MODE_PRIVATE);
        readSP();
        //3
        initRecyclerView();
        //实例化model 然后调用方法  获取网络数据
        //4实例化
        ArticleModel articleModel = new ArticleModel();
        articleModel.getResultList("article",page,"k17vmm42ui9al6clh0lo3de2t7",articleListener);
        //返回动态生成的view
        return view;
    }

    private void readSP() {
        sessionID=sp.getString("sessionID",null);
    }


}
